<?php

namespace App\Http\Requests\Admin\Panx\Customer;

use App\Http\Requests\ApiRequest;
use App\Http\Requests\Concerns\CustomerRule;
use App\Models\Customer\Customer;
use App\Services\Locale\Language;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

/**
 * @property string account - customer_admins 账号
 * @property string password
 * @property string phone
 * @property string name
 * @property string legal_person
 * @property string telephone
 * @property string contacts
 * @property string license
 * @property string address
 * @property string division_percent
 * @property string|null provider_type
 * @property string authorizer_id
 * @property string email
 * @property array operate_category
 * @property int|null has_hair
 * @property int dev_acnt_num - 机器开通数量
 * @property string province
 * @property string city
 * @property string district
 * @property string language
 * @property string water_mark
 * @property string has_youzan
 * @property string youzan_authorizer_id
 * @property string is_debug
 */
class CustomerStoreRequest extends ApiRequest
{
    use CustomerRule;

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {


        $data = [
            'name'               => 'required|string|unique:customers,name|max:'.self::MAX_STRING,
            'account'            => 'required|string|unique:customer_admins,account|max:'.self::MAX_STRING,
            'password'           => 'required|string|min:6|max:'.self::MAX_STRING,
            'legal_person'       => 'required|string|max:'.self::MAX_STRING,
            'contacts'           => 'required|string|max:'.self::MAX_STRING,
            'license'            => 'nullable|file|max:2048',
            'address'            => 'required|string|max:'.self::MAX_STRING,
//            'province'           => 'required|string',
//            'city'               => 'required|string',
//            'district'           => 'required|string',
//            'authorizer_id'      => 'nullable|int|min:0',
            'email'              => 'nullable|email|unique:customer_admins,email',
//            'division_percent'   => 'required|numeric',
            'provider_type'      => ['required', 'int', Rule::in(Customer::VALIDATE_PROVIDER_TYPES)],
            'operate_category'   => 'required|array',
            'operate_category.*' => 'required|int|min:1|max:'.self::MAX_INT,
//            'has_hair'           => 'required|numeric|in:0,1',
            'dev_acnt_num'       => "required_if:provider_type, " . Customer::PROVIDER_TYPE_SOFTWARE . "|int|min:0|max:".self::MAX_INT,
            'language'           => ['required','string', Rule::in(Language::localDirNames())],
            'water_mark'         => 'nullable|file|max:2048',
            'show_virtual'       => "required_unless:provider_type," . Customer::PROVIDER_TYPE_VIRTUAL . "|bool",
            'telephone'          => ['required', 'string', 'max:'.self::MAX_STRING],
            'phone'              => ['required', 'string', 'max:'.self::MAX_STRING],
            'is_debug'           => 'nullable|int|in:0,1'

//            'has_youzan'         => 'nullable|int|in:0,1',
        ];

//        if ($this->language === Language::ZH_CN) {
//            $data['telephone'] = ['required', 'regex:/^1[34578][0-9]{9}$|^((0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/'];
//            $data['phone'] = ['required', 'regex:/^1[34578][0-9]{9}$|^((0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/'];
//        } else {
//            $data['telephone'] = ['required', 'string'];
//            $data['phone'] = ['required', 'string'];
//        }
        $provider_type_rule = $this->providerTypeRule((int)$this->provider_type);

        return $provider_type_rule + $data;
    }

    /**
     * @return array
     */
//    public function messages()
//    {
//        return [
//            'name.*'               => '公司名称必须是唯一的字符串',
//            'account.*'            => '超管账号必须是唯一的字符串',
//            'password.*'           => '超管账号密码长度必须是6个字符',
//            'legal_person.*'       => '公司法人必须是字符串',
//            'telephone.*'          => '公司电话必须是合法手机号或电话',
//            'contacts.*'           => '公司联系人必须是唯一的字符串',
//            'phone.*'              => '联系人电话必须是合法手机号',
//            'license.*'            => '营业执照必须是小于2M的上传图片',
//            'address.*'            => '公司地址必须是字符串',
//            'division_percent.*'   => '分成比例必须是数值',
//            'provider_type.*'      => '开通账户类型必须是数值1或2或3',
//            'authorizer_id.*'      => '客户绑定的公众号必须是授权公众号ID',
//            'email.*'              => '邮箱必须合法',
//            'operate_category.*.*' => '开通的产品类别的值必须是数值',
//            'operate_category.*'   => '开通的产品类别必须是数组',
//            'has_hair.*'           => '是否开通发型试戴的值必须是0或1',
//            'show_virtual.*'       => '是否开启虚拟仓的值必须是0或1',
//            'dev_acnt_num.*'       => '购买软件的客户开通设备账号个数必须是大于0的数值',
//        ];
//    }
}
